package org.example.tails.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.tails.mapper.empMapper;
import org.example.tails.pojo.Emp;
import org.example.tails.pojo.LoginInfo;
import org.example.tails.service.LoginService;
import org.example.tails.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private empMapper empmapper;

    @Override
    public LoginInfo login(Emp emp) {
        Emp e=empmapper.selectByUsernameAndPassword(emp);
        if(e!=null){
            log.info("登陆成功，员工姓名{}",e.getName());
            Map<String,Object>map=new HashMap<>();
            map.put("id",e.getId());
            map.put("username",e.getUsername());
            String token= JwtUtil.generateToken(map);
            LoginInfo info=new LoginInfo(e.getId(),e.getUsername(),e.getName(),token);
            return info;
        }
        return null;
    }
}
